package com.maya.medicalwastes.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * *
 */
public class DateUtilA {

    /**
     * date->time
     *
     * @param expireDate
     * @return
     */
    public static long getSecondsFromDate(String expireDate) {
        if (expireDate == null || expireDate.trim().equals(""))
            return 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(expireDate);
            return (long) (date.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0L;
        }
    }

    public static int getDiffMinutes(String olderTime, String newTime) {
        long olderSeconds = getSecondsFromDate(olderTime);
        long newSeconds = getSecondsFromDate(newTime);
        int minutes = 0;
        minutes = (int) ((newSeconds - olderSeconds) / 60);
        return minutes;
    }

    public static int getDiffSeconds(String olderTime, String newTime) {
        long olderSeconds = getSecondsFromDate(olderTime);
        long newSeconds = getSecondsFromDate(newTime);
        int seconds = 0;
        seconds = (int) ((newSeconds - olderSeconds));
        return seconds;
    }

    public static int getRemainTime(String startTime, int duration) {
        String currentTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
        int diffSeconds = getDiffSeconds(startTime, currentTime);
        int distanceSeconds = duration - diffSeconds;
        if (distanceSeconds <= 0) {
            return 0;
        } else {
            return distanceSeconds;
        }
    }

    //日期字符串转换Date实体
    public static Date parseServerTime(String serverTime, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        Date date = null;
        try {
            date = sdf.parse(serverTime);
        } catch (Exception e) {

        }
        return date;
    }

    //Date对象获取时间字符串
    public static String getDateStr(Date date, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    public static String ParseStrFormatTime(String beforeTime) {
        if (beforeTime == null || beforeTime.isEmpty()) {
            return "";
        }
        String currentTime = DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
        Date date = parseServerTime(beforeTime, "yyyy-MM-dd HH:mm:ss");
        return getDateStr(date, "HH:mm");
    }

    /**
     * 是否在时间段内
     */
    public static boolean isInThatTime(String beginTimeStr, String endTimeStr) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        //初始化
        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            //格式化当前时间格式
            nowTime = df.parse(df.format(new Date()));
            //定义开始时间
            beginTime = df.parse(beginTimeStr);
            //定义结束时间
            endTime = df.parse(endTimeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //调用判断方法
        boolean flag = belongCalendar(nowTime, beginTime, endTime);
        return flag;
        //输出为结果
/*        if (flag) {
            //处于规定的时间段内，执行对应的逻辑代码
            System.out.println(flag);
        } else {
            //不处于规定的时间段内，执行对应的逻辑代码
            System.out.println(flag);
        }*/

    }

    public static boolean isInThatTime(String selectTime, String beginTimeStr, String endTimeStr) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        //初始化
        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            //格式化当前时间格式
            nowTime = df.parse(selectTime);
            //定义开始时间
            beginTime = df.parse(beginTimeStr);
            //定义结束时间
            endTime = df.parse(endTimeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //调用判断方法
        boolean flag = belongCalendar(nowTime, beginTime, endTime);
        return flag;
    }


    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        //设置结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        //处于开始时间之后，和结束时间之前的判断
        if (date.after(begin) && date.before(end)) {
            return true;
        } else if (date.equals(begin) || date.equals(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static String strToStr(String date, String format) {
        Date date1 = DateUtils.parseDateStr(date, "yyyy-MM-dd HH:mm:ss");
        String timeByFromat = DateUtils.getTimeByFromat(date1, format);
        return timeByFromat;
    }

    // 根据
    public static String parseTimeByVali(String planTime, int validityType) {

        String tempTime = FormatUtils.getObject(planTime);
        String newTime = StringUtils.isEmpty(tempTime) ? tempTime : DateUtilA.strToStr(tempTime, "yyyy-MM-dd HH:mm");
        String newTimeNotHaveYear = newTime.substring(5,16);
        String time = validityType == 3 ? "随时" : newTimeNotHaveYear;

        return time;
    }

    public static String getTomorrowDate() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    // 根据
    public static String parseTimeByValiFormat(String planTime, int validityType, String format) {

        String tempTime = FormatUtils.getObject(planTime);
        String newTime = StringUtils.isEmpty(tempTime) ? tempTime : DateUtilA.strToStr(tempTime, format);

        String time = validityType == 3 ? "随时" : newTime;

        return time;
    }

}
